package com.mingzhi.order;

import com.alibaba.cloud.nacos.NacosConfigManager;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.Listener;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@EnableFeignClients //开启feign远程调用功能
@EnableDiscoveryClient //开启服务发现功能
@SpringBootApplication
public class OrderMainApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderMainApplication.class, args);
    }

    //1、项目一启动就监听配置文件的变化
    //2、发生变化后拿到变化的值
    @Bean
    public ApplicationRunner applicationRunner(NacosConfigManager nacosConfigManager) {
        return args -> {
            System.out.println("===项目启动成功===");
            //拿到配置服务
            ConfigService configService = nacosConfigManager.getConfigService();
            /**
             * 添加监听器
             * 参数1：数据id
             * 参数2：组id
             * 参数3：监听器
             */
            configService.addListener("service-order.properties",
                    "DEFAULT_GROUP",
                    new Listener() {
                        /**
                         * 线程池
                         * @return
                         */
                        @Override
                        public Executor getExecutor() {
                            //返回一个固定大小的线程池
                            return Executors.newFixedThreadPool(4);
                        }

                        /**
                         * 接受配置信息
                         * @param configInfo
                         */
                        @Override
                        public void receiveConfigInfo(String configInfo) {
                            System.out.println("变化的配置信息：" + configInfo);
                            System.out.println("进行邮件通知");
                        }
                    });
        };
    }
}
